package com.tedu.web;

import com.tedu.pojo.News;
import com.tedu.pojo.NewsTopic;
import com.tedu.servies.NewsServies;
import com.tedu.servies.NewsServiesImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 新闻模块  web   处理新闻相关  请求
 */
public class NewsServlet extends HttpServlet {

    NewsServies newsServies;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取用户请求类型
        String name = req.getParameter("name");
        //实例化
        newsServies = new NewsServiesImpl();
        try{
            if(name.equals("index")){
                //显示新闻首页
                //获取全部新闻类型
                List<NewsTopic> allTopic = newsServies.getAllTopic();
                //分页获取新闻列表
                List<News> newsByPageNo = newsServies.getNewsByPageNo(1, 100);

                //发送数据到页面
                req.setAttribute("allTopic",allTopic);
                req.setAttribute("newsByPageNo",newsByPageNo);

                //转发到首页
                req.getRequestDispatcher("/index.jsp").forward(req,resp);

            }
        }catch (Exception e){
            e.printStackTrace();
        }



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        this.doGet(req,resp);
    }
}
